using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Base;
using UnityEngine;

public class MapManager : MonoBehaviour
{
    // 地图大小（假设是正方形地图）
    public int mapSize = 10;
    
    // 当前中心地图的坐标索引（0,0）是初始中心
    private Vector3 currentCenterIndex = Vector3.zero;
    
    private Vector2Int[] currentMaps = new Vector2Int[9];
    
    // 所有地图的字典，键是坐标索引
    private GameObject[] mapTiles = new GameObject[9];
    
    // 主角引用
    [SerializeField]
    public Transform player;
    [SerializeField]
    public GameObject MapTilePrefab;

    void Start()
    {
        EventCenterManager.Instance.AddListener<Transform>(EventCommand.MapMove,OnMapMoveClick);

        InitializeMaps();
    }
    private void OnMapMoveClick(Transform t)
    {
        if (t.position == currentCenterIndex)
        {
            return;
        }
        currentCenterIndex = t.position;
        Debug.Log(currentCenterIndex);
        CheckPlayerPosition();
    }
    
    private void InitializeMaps()
    {
        int index = 0;
        // 初始化9张地图，按照九宫格排列
        for (int x = -1; x <= 1; x++)
        {
            for (int y = -1; y <= 1; y++)
            {
                // 这里假设你已经预先放置了地图或动态生成了地图
                GameObject map = Instantiate(MapTilePrefab, transform);
                mapTiles[index] = map;
                map.transform.position = new Vector3(x * mapSize, y * mapSize, 0);
                currentMaps[index] = new Vector2Int(x * mapSize, y * mapSize);
                index++;
            }
        }
    }
    private void CheckPlayerPosition()
    {
        // 计算玩家相对于当前中心地图的位置
        int index = 0;
        // 初始化9张地图，按照九宫格排列
        for (int x = -1; x <= 1; x++)
        {
            for (int y = -1; y <= 1; y++)
            {
                // 这里假设你已经预先放置了地图或动态生成了地图
                mapTiles[index].transform.position = new Vector3(x * mapSize+currentCenterIndex.x, y * mapSize+currentCenterIndex.y, 0);
                index ++;
            }
        }
    }
}
